package com.ymatou.messageroute.api.testcase;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.messageroute.api.parameter.GetScenarioMessageBean;
import com.ymatou.messageroute.api.parameter.ImMessageBean;
import com.ymatou.messageroute.api.parameter.MessageTaskBean;
import com.ymatou.messageroute.api.parameter.MessageToolTaskBean;
import com.ymatou.messageroute.api.service.GetScenarioMessageCall;
import com.ymatou.operate.trading.api.parameter.ChangeOrderAddressBean;
import com.ymatou.operate.trading.api.service.ChangeOrderAddressCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.enums.BusinessTypeEnum;
import com.ymttest.common.enums.CancelOrderTypeEnum;
import com.ymttest.common.enums.UserTypeEnum;
import com.ymttest.database.model.YmtIdPic;
import com.ymttest.database.sqlwapper.YmtIdCardWapper;
import com.ymttest.database.sqlwapper.YmtIdPicWapper;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 *  
 */
@TestSuite
public class Ts_MessageRoute_buyer {
	private static OrderCallService orderService;
	private static GetScenarioMessageCall scenarioMessageCall;
	MessageTaskBean messageTask;
	List<MessageToolTaskBean> messageToolTaskList;
	ImMessageBean imMessage;
	static Map<String, Comparable> user;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("消息路由接口");
		user = getUser();
	}

	@Before
	public void caseUp() {
		orderService = new OrderCallService();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(scenarioMessageCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 根据订单号，用户id，日期，查询订单发送的消息
	 * @param orderId 订单号
	 * @param userId 用户id
	 * @param messageDueTime 日期 yyyy-MM-dd
	 * @return GetScenarioMessageCall
	 */
	private static GetScenarioMessageCall getScenarioMessageCallService(
			int orderId, int userId, String messageDueTime,
			BusinessTypeEnum type) {
		GetScenarioMessageBean getScenarioMessageBean = new GetScenarioMessageBean();
		GetScenarioMessageCall getScenarioMessageCall = new GetScenarioMessageCall();
		try {
			getScenarioMessageBean.setBusinessType(type);
			getScenarioMessageBean.setOrderId(orderId);
			getScenarioMessageBean.setUserId(userId);
			getScenarioMessageBean.setMessageDueTime(messageDueTime);
			getScenarioMessageCall.setData(getScenarioMessageBean);
			getScenarioMessageCall.callService();
			Thread.sleep(2000);
			Logger.debug("getScenarioMessage call:"
					+ getScenarioMessageCall.getEntityString());
			Logger.debug("getScenarioMessage response:"
					+ getScenarioMessageCall.getReturnData());
		} catch (Exception e) {
			Logger.fail(e);
		}
		return getScenarioMessageCall;
	}

	/**
	 * 测试数据
	 * 
	 * @return user date map
	 */
	private static Map getUser() {
		Map<String, Comparable> user = new HashMap();
		user.put("UserId",
				Integer.valueOf(EnvSetup.getData("MessageRouteUserId")));
		user.put("UserName", EnvSetup.getData("MessageRouteName"));
		user.put("Phone", EnvSetup.getData("MessageRoutePhone"));
		return user;
	}

	/**
	 * 创造是否上传身份信息
	 * 
	 * @param isUpLoad
	 *            是否要上传身份证
	 */
	private static void createIdCardInfo(boolean isUpLoad) {
		YmtIdCardWapper idCard = new YmtIdCardWapper();
		YmtIdPicWapper idPic = new YmtIdPicWapper();
		YmtIdPic idPicRecord = new YmtIdPic();
		Integer userId = (Integer) user.get("userId");
		idPicRecord.setSid(TestDataManager.getRandomUUID().toString());
		idPicRecord.setIuserid(userId);
		idPicRecord.setSname((String) user.get("userName"));
		idPicRecord.setIaction(1);
		idPicRecord.setScardid("110101200008019536");
		idPicRecord
				.setSidpicrightside("http://file.ymatou.com/files/IdCard/32adc978-907e-4068-8497-8fac8f41c907.jpg");
		idPicRecord
				.setSidpicreverseside("http://file.ymatou.com/files/IdCard/3e7314c5-3c83-46a0-a3a4-a92e7dbbf20c.jpg");
		try {
			// 要求要上传身份证
			if (isUpLoad) {
				// 改用户未上传
				if (idPic.selectIdPicByiUserId(userId.toString()).size() == 0) {
					// 添加一条数据
					idPic.insertIdPic(idPicRecord);
				}
			}
			// 要求没有上传身份证
			else {
				// 该用户已上传
				if (idPic.selectIdPicByiUserId(userId.toString()).size() > 0) {
					// 删除信息
					idPic.deleterIdPicByUserId(userId.toString());
				}
			}
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MessageRoute_buyer_001() {
		Logger.start(true, "m2c订单已取消(用户取消)");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int userId = placeOrderResp.getUserId();
			// 取消
			orderService.cancelOrder(orderId, userId,
					CancelOrderTypeEnum.BuyerCancelOrder);
			Logger.comment("前置数据准备结束");
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			messageTask = scenarioMessageCall.getMessageTask();
			// 验证订单未支付前，用户取消，不发通知信息
			Logger.verifyIsNull(messageTask, "验证订单未支付前,用户取消,不发通知信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MessageRoute_buyer_002() {
		Logger.start(true, "m2c订单已取消(商家取消)");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int userId = placeOrderResp.getUserId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			String productName = null;
			// 产品名称超过20位，截取20位
			if (productName.length() > 20) {
				productName = productName.substring(0, 20);
			}
			// 取消
			orderService.cancelOrder(orderId, sellerId,
					CancelOrderTypeEnum.SellerCancelOrder);
			Logger.comment("前置数据准备结束");
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			Collections.reverse(messageToolTaskList);
			// 短信
			Logger.verifyEquals(1, messageToolTaskList.get(1).getMessageTool(),
					"验证m2c订单已取消，发短信");
			Logger.verifyIsContains(ECode.Message_Route_01, messageToolTaskList
					.get(1).getMessageBody(), "验证m2c订单已取消,短信内容包含"
					+ ECode.Message_Route_01);
			Logger.verifyEquals(true, messageToolTaskList.get(1).getSuccess(),
					"验证短信发送成功");
			// app 消息
			Logger.verifyEquals(2, messageToolTaskList.get(0).getMessageTool(),
					"验证m2c订单已取消，发app消息");
			Logger.verifyIsContains(ECode.Message_Route_01,
					imMessage.getTitle(), "验证m2c订单已取消,短信内容包含"
							+ ECode.Message_Route_01);
			Logger.verifyEquals(true, messageToolTaskList.get(0).getSuccess(),
					"验证消息发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}

	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MessageRoute_buyer_003() {
		Logger.start(true, "m2c订单已取消(系统取消)");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int userId = placeOrderResp.getUserId();
			String productName = "autotestxxxx";
			// 产品名称超过20位，截取20位
			if (productName.length() > 20) {
				productName = productName.substring(0, 20);
			}
			// 系统取消
			orderService.cancelOrder(orderId, userId,
					CancelOrderTypeEnum.SystemCancelOrder);
			Logger.comment("前置数据准备结束");
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			Collections.reverse(messageToolTaskList);
			Logger.verifyEquals(1, messageToolTaskList.size(),
					"验证m2c订单取消(系统取消)，只发app消息");
			// app 消息
			Logger.verifyEquals(2, messageToolTaskList.get(0).getMessageTool(),
					"验证m2c订单已取消，发app消息");
			Logger.verifyIsContains(ECode.Message_Route_02,
					imMessage.getTitle(), "验证m2c订单已取消,app消息内容包含"
							+ ECode.Message_Route_02);
			Logger.verifyEquals(true, messageToolTaskList.get(0).getSuccess(),
					"验证消息发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MessageRoute_buyer_005() {
		Logger.start(true, "m2c支付后取消(商家取消)");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			BigDecimal totalPrice = placeOrderResp.getPayAmount();
			int tradingId = placeOrderResp.getTradingId();
			// 支付
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 取消
			orderService.cancelOrder(orderId, sellerId,
					CancelOrderTypeEnum.SellerCancelOrder);
			Logger.comment("前置数据准备结束");
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			String productName = "autotestxxxx";
			// 产品名称超过20位，截取20位
			if (productName.length() > 20) {
				productName = productName.substring(0, 20);
			}
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			//对 messageToolTaskList排序
			Collections.reverse(messageToolTaskList);
			imMessage = scenarioMessageCall.getImMessage();
			Collections.reverse(messageToolTaskList);
			String dueTime = messageTask.getDueTime().replace("T", " ");
			int diffMin = YMTDateUtil.diffMinute(new Date(),
					new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dueTime));
			// 验证实时发送
			Logger.verifyEquals(true, Math.abs(diffMin) < 5,
					"验证m2c订单已取消(买手取消)，实时发送消息");
			// 验证发送2个渠道
			Logger.verifyEquals(messageToolTaskList.size(), 2,
					"验证m2c订单取消(买手取消)，发送2个渠道");
			// 短信
			Logger.verifyEquals(1, messageToolTaskList.get(1).getMessageTool(),
					"验证m2c订单已取消，发短信");
			Logger.verifyEquals("哈尼，您购买的订单（含" + productName
					+ "等商品）被买手取消，您所支付的款项已退回到您的洋码头账户余额中，如有问题可联系买手咨询。",
					messageToolTaskList.get(1).getMessageBody(),
					"验证m2c订单已取消,短信内容");
			Logger.verifyEquals(true, messageToolTaskList.get(1).getSuccess(),
					"验证短信发送成功");
			// app 消息
			Logger.verifyEquals(2, messageToolTaskList.get(0).getMessageTool(),
					"验证m2c订单已取消，发app消息");
			Logger.verifyIsContains(ECode.Message_Route_03,
					imMessage.getTitle(), "验证m2c订单已取消,app消息内容包含"
							+ ECode.Message_Route_03);
			Logger.verifyEquals(true, messageToolTaskList.get(0).getSuccess(),
					"验证消息发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MessageRoute_buyer_006() {
		Logger.start(true, "m2c支付后取消(风控取消)");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			String productId = "sssssssssssssssss";

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 买家付款
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 风控取消
			Logger.comment("风控检测未通过");
			YmatouEasyCall RiskControlNotifyCall = new YmatouEasyCall("",
					"POST", "JSON");
			String RiskControlNotifyString = "{OrderId:" + orderId
					+ ",ParamExt:{\"Status\":\"rejected\"}}";
			RiskControlNotifyCall
					.setUrl("http://api.trading.operate.ymatou.com/api/Notify/RiskControlNotify");
			RiskControlNotifyCall.setData(RiskControlNotifyString);
			RiskControlNotifyCall.callService();
			Logger.comment("前置数据准备结束");
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			String productName = "autotestxxxx";
			// 产品名称超过20位，截取20位
			if (productName.length() > 20) {
				productName = productName.substring(0, 20);
			}
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			Collections.reverse(messageToolTaskList);
			String dueTime = messageTask.getDueTime().replace("T", " ");
			int diffMin = YMTDateUtil.diffMinute(new Date(),
					new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dueTime));
			// 验证实时发送
			Logger.verifyEquals(true, Math.abs(diffMin) < 5,
					"验证m2c订单已取消(风控取消)，实时发送消息");
			// 验证发送2个渠道
			Logger.verifyEquals(messageToolTaskList.size(), 2,
					"验证m2c订单取消(风控取消)，发送2个渠道");
			// 短信
			Logger.verifyEquals(2, messageToolTaskList.get(1).getMessageTool(),
					"验证m2c订单已取消，发短信");
			Logger.verifyIsContains(ECode.Message_Route_04, messageToolTaskList
					.get(1).getMessageBody(), "验证m2c订单已取消,短信内容含"
					+ ECode.Message_Route_04);
			Logger.verifyEquals(messageToolTaskList.get(1).getSuccess(), true,
					"验证短信发送成功");
			// app 消息
			Logger.verifyEquals(1, messageToolTaskList.get(0).getMessageTool(),
					"验证m2c订单已取消，发app消息");
			Logger.verifyIsContains(ECode.Message_Route_04,
					imMessage.getTitle(), "验证m2c订单已取消,app消息内容含"
							+ ECode.Message_Route_04);
			Logger.verifyEquals(messageToolTaskList.get(0).getSuccess(), true,
					"验证消息发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_MessageRoute_buyer_007() {
		Logger.start(true, "m2c支付后取消(用户取消)");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int userId = placeOrderResp.getUserId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			BigDecimal totalPrice = placeOrderResp.getPayAmount();
			int tradingId = placeOrderResp.getTradingId();
			// 支付
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 修改订单时间为2天前，让用户可以取消
			new YmtOrdersWapper().updateOrderAddtimeByOrderId(orderId, -3);
			// 用户取消
			orderService.cancelOrder(orderId, userId,
					CancelOrderTypeEnum.BuyerCancelOrder);
			Logger.comment("前置数据准备结束");
			// 验证推送消息
			String productName = "autotestxxxx";
			// 产品名称超过20位，截取20位
			if (productName.length() > 20) {
				productName = productName.substring(0, 20);
			}
			// call message service
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			Collections.reverse(messageToolTaskList);
			// 验证发送2个渠道
			Logger.verifyEquals(2, messageToolTaskList.size(),
					"验证m2c订单取消(用户取消)，发送2个渠道");
			// 短信
			Logger.verifyEquals(1, messageToolTaskList.get(1).getMessageTool(),
					"验证m2c订单已取消，发短信");
			Logger.verifyEquals("哈尼，订单（含" + productName
					+ "等商品）取消成功，您所支付的款项已退回到您的洋码头账户余额中，", messageToolTaskList
					.get(1).getMessageBody(), "验证m2c订单已取消,短信内容");
			Logger.verifyEquals(true, messageToolTaskList.get(1).getSuccess(),
					"验证短信发送成功");
			// app 消息
			Logger.verifyEquals(2, messageToolTaskList.get(0).getMessageTool(),
					"验证m2c订单已取消，发app消息");
			Logger.verifyEquals("哈尼，订单取消成功，您所支付的款项已退回到您的洋码头账户余额中，",
					imMessage.getTitle(), "验证m2c订单已取消,app消息内容");
			Logger.verifyEquals(true, messageToolTaskList.get(0).getSuccess(),
					"验证消息发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_MessageRoute_buyer_008() {
		Logger.start(true, "m2c卖家已接单(用户未传身份证)");
		try {
			// 用户未传身份证
			//createIdCardInfo(false);
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			// 支付
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			int tradingId = placeOrderResp.getTradingId();
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 卖家接单
			orderService.acceptOrder(orderId, sellerId);
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.AcceptOrder);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.AcceptOrder);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			String dueTime = messageTask.getDueTime().replace("T", " ");
			int diffMin = YMTDateUtil.diffMinute(new Date(),
					new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dueTime));
			// 验证实时发送
			Logger.verifyEquals(true, Math.abs(diffMin) < 5,
					"验证m2c卖家已接单(用户未传身份证)，实时发送消息");
			// 验证发送1个渠道
			Logger.verifyEquals(messageToolTaskList.size(), 1,
					"验证m2c卖家已接单(用户未传身份证),只发app消息");
			// app 消息
			Logger.verifyEquals(2, messageToolTaskList.get(0).getMessageTool(),
					"验证m2c卖家已接单(用户未传身份证)，发app消息");
			Logger.verifyEquals(imMessage.getTitle(),
					"哈尼，购买的商品已经接单，赶紧去“订单中心”上传身份证吧，海关叔叔在等呢",
					"验证m2c卖家已接单(用户未传身份证),app消息内容");
			Logger.verifyEquals(messageToolTaskList.get(0).getSuccess(), true,
					"验证消息发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_MessageRoute_buyer_009() {
		Logger.start(true, "m2c卖家已接单(用户已传身份证)");
		try {
			// 用户已传身份证
			createIdCardInfo(true);
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			// 支付
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			int tradingId = placeOrderResp.getTradingId();
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 卖家接单
			orderService.acceptOrder(orderId, sellerId);
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.AcceptOrder);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.AcceptOrder);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			String dueTime = messageTask.getDueTime().replace("T", " ");
			int diffMin = YMTDateUtil.diffMinute(new Date(),
					new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dueTime));
			// 验证实时发送
			Logger.verifyEquals(true, Math.abs(diffMin) < 5,
					"验证m2c卖家已接单(用户已传身份证)，实时发送消息");
			// 验证发送1个渠道
			Logger.verifyEquals(messageToolTaskList.size(), 1,
					"验证m2c卖家已接单(用户已传身份证),只发app消息");
			// app 消息
			Logger.verifyEquals(2, messageToolTaskList.get(0).getMessageTool(),
					"m2c卖家已接单(用户已传身份证)，发app消息");
			Logger.verifyEquals(imMessage.getTitle(),
					"哈尼，购买的商品已经接单，耐心等待卖家采购吧~", "验证m2c卖家已接单(用户已传身份证),app消息内容");
			Logger.verifyEquals(messageToolTaskList.get(0).getSuccess(), true,
					"验证消息发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_MessageRoute_buyer_010() {
		Logger.start(true, "m2c卖家已接单后取消(商家取消)");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			// 支付
			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			int tradingId = placeOrderResp.getTradingId();
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 卖家接单
			orderService.acceptOrder(orderId, sellerId);
			// 取消
			orderService.cancelOrder(orderId, sellerId,
					CancelOrderTypeEnum.SellerCancelOrder);
			Logger.comment("前置数据准备结束");
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			String productName = "autotestxxxx";
			// 产品名称超过20位，截取20位
			if (productName.length() > 20) {
				productName = productName.substring(0, 20);
			}
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.CancelOrder);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			Collections.reverse(messageToolTaskList);
			String dueTime = messageTask.getDueTime().replace("T", " ");
			// 验证发送2个渠道
			Logger.verifyEquals(messageToolTaskList.size(), 2,
					"验证m2c卖家已接单后取消(买手取消)，发送2个渠道");
			// 短信
			Logger.verifyEquals(1, messageToolTaskList.get(1).getMessageTool(),
					"验证m2c卖家已接单后取消(买手取消)，发短信");
			Logger.verifyEquals("哈尼，您购买的订单（含" + productName
					+ "等商品）被买手取消，您所支付的款项已退回到您的洋码头账户余额中，如有问题可联系买手咨询。",
					messageToolTaskList.get(1).getMessageBody(),
					"验证m2c卖家已接单后取消(买手取消),短信内容");
			Logger.verifyEquals(true, messageToolTaskList.get(1).getSuccess(),
					"验证短信发送成功");
			// app 消息
			Logger.verifyEquals(2, messageToolTaskList.get(0).getMessageTool(),
					"验证m2c卖家已接单后取消(买手取消)，发app消息");
			Logger.verifyEquals(
					"哈尼，您购买的订单被买手取消，您所支付的款项已退回到您的洋码头账户余额中，如有问题可联系买手咨询。",
					imMessage.getTitle(), "验证m2c订单已取消,app消息内容");
			Logger.verifyEquals(true, messageToolTaskList.get(0).getSuccess(),
					"验证消息发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MessageRoute_buyer_011() {
		Logger.start(true, "m2c商家已发货");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int SellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			int tradingId = placeOrderResp.getTradingId();
			// 买家付款
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderService.acceptOrder(orderId, SellerId);
			// 商家发货
			orderService.dispatched(orderId, SellerId);
			Logger.comment("前置数据准备结束");
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			String productName = "autotestxxxx";
			// 产品名称超过20位，截取20位
			if (productName.length() > 20) {
				productName = productName.substring(0, 20);
			}
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.SellerDispatched);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.SellerDispatched);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			// 验证发送1个渠道
			Logger.verifyEquals(1, messageToolTaskList.size(),
					"验证m2c卖家已发货,只发app消息");
			// app 消息
			Logger.verifyEquals(2, messageToolTaskList.get(0).getMessageTool(),
					"验证m2c卖家已发货，发app消息");
			Logger.verifyIsContains(ECode.Message_Route_05,
					imMessage.getTitle(), "验证m2c卖家已发货,app消息内容包含"
							+ ECode.Message_Route_05);
			Logger.verifyEquals(true, messageToolTaskList.get(0).getSuccess(),
					"验证消息发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_MessageRoute_buyer_012() {
		Logger.start(true, "m2c支付后10分钟未上传身份证");
		try {
			// 用户未传身份证
			// createIdCardInfo(false);
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int SellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 买家付款
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.OrderPay);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.OrderPay);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			Collections.reverse(messageToolTaskList);
			String dueTime = messageTask.getDueTime().replace("T", " ");
			int diffMin = YMTDateUtil.diffMinute(new Date(),
					new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dueTime));
			// 验证实时发送
			Logger.verifyEquals(true, Math.abs(diffMin) < 5,
					"验证m2c支付尾款后10分钟未上传身份证，实时发送消息");
			// 短信
			Logger.verifyEquals(1, messageToolTaskList.get(1).getMessageTool(),
					"验证m2c支付尾款后10分钟未上传身份证，发短信");
			Logger.verifyEquals(
					"哈尼，请上传身份证信息，这是清关时的必要信息，赶紧登录APP“订单中心”或(www.ymatou.com/b）上传信息，如已上传请忽略。",
					messageToolTaskList.get(1).getMessageBody(),
					"验证m2c支付尾款后10分钟未上传身份证,短信内容");
			// app 消息
			Logger.verifyEquals(2, messageToolTaskList.get(0).getMessageTool(),
					"验证m2c支付尾款后10分钟未上传身份证，发app消息");
			Logger.verifyEquals("哈尼，请上传身份证信息，这是清关时的必要信息，赶紧去\"订单中\"上传吧，如已上传请忽略",
					imMessage.getTitle(), "验证m2c支付尾款后10分钟未上传身份证,app消息内容");
			// 消息10分钟后推送
			// Thread.sleep(600000);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.OrderPay);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			Logger.verifyEquals(true, messageToolTaskList.get(0).getSuccess(),
					"验证消息发送成功");
			Logger.verifyEquals(true, messageToolTaskList.get(1).getSuccess(),
					"验证短信发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	// @Test
	@Category(P1.class)
	@TestCase
	public void Tc_MessageRoute_buyer_013() {
		Logger.start(true, "m2c支付后10分钟内已上传身份证");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int SellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 买家付款
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// call message service
			// 设置用户已传身份证
			//createIdCardInfo(true);
			// 10分中后
			Thread.sleep(600000);
			// 验证推送消息
			Logger.comment("前置数据准备结束");
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.OrderPay);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.OrderPay);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			Collections.reverse(messageToolTaskList);
			// 短信
			Logger.verifyEquals(1, messageToolTaskList.get(1).getMessageTool(),
					"验证m2c支付尾款后10分钟未上传身份证，发短信");
			Logger.verifyEquals(
					"哈尼，请上传身份证信息，这是清关时的必要信息，赶紧登录APP\"订单中心\"或(www.ymatou.com/b）上传信息，如已上传请忽略。",
					messageToolTaskList.get(1).getMessageBody(),
					"验证m2c支付尾款后10分钟未上传身份证,短信内容");
			Logger.verifyEquals(false, messageToolTaskList.get(1).getSuccess(),
					"验证不发送短信");
			// app 消息
			Logger.verifyEquals(2, messageToolTaskList.get(0).getMessageTool(),
					"验证m2c支付尾款后10分钟未上传身份证，发app消息");
			Logger.verifyEquals("哈尼，请上传身份证信息，这是清关时的必要信息，赶紧去“订单中心”上传吧，如已上传请忽略",
					imMessage.getTitle(), "验证m2c支付尾款后10分钟未上传身份证,app消息内容");
			Logger.verifyEquals(false, messageToolTaskList.get(0).getSuccess(),
					"验证不发送消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_MessageRoute_buyer_014() {
		Logger.start(true, "m2c确认收货");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int SellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 买家付款
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderService.acceptOrder(orderId, SellerId);
			// 商家发货
			orderService.dispatched(orderId, SellerId);
			// 确认收货
			orderService.confirmReceive(SellerId, orderId);
			Logger.comment("前置数据准备结束");
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.ConfirmReceive);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.ConfirmReceive);
			messageTask = scenarioMessageCall.getMessageTask();
			Logger.verifyIsNull(messageTask, "验证m2c确认收货货,不发消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MessageRoute_buyer_015() {
		Logger.start(true, "m2c商家延期发货");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int SellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 买家付款
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderService.acceptOrder(orderId, SellerId);
			// 商家发货
			orderService.dispatched(orderId, SellerId);
			// 商家延期发货
			int delayDay = new Random().nextInt(10);
			orderService.sellerDelayReceiveTime(orderId, SellerId, delayDay);
			Logger.comment("前置数据准备结束");
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			String productName = "autotestxxxx";
			// 产品名称超过20位，截取20位
			if (productName.length() > 20) {
				productName = productName.substring(0, 20);
			}
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.SellerDelayReceive);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.SellerDelayReceive);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			// app 消息
			Logger.verifyEquals(1, messageToolTaskList.size(),
					"验证商家延期发货，只发发app消息");
			Logger.verifyEquals(2, messageToolTaskList.get(0).getMessageTool(),
					"验证商家延期发货，发app消息");
			Logger.verifyIsContains(ECode.Message_Route_06,
					imMessage.getTitle(), "验证m2c卖家延迟发货,app消息内容包含"
							+ ECode.Message_Route_06);
			Logger.verifyEquals(true, messageToolTaskList.get(0).getSuccess(),
					"验证消息发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MessageRoute_buyer_016() {
		Logger.start(true, "m2c买家延期收货");
		try {
			Logger.comment("前置数据准备");
			// 创建订单
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 买家付款
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderService.acceptOrder(orderId, sellerId);
			// 商家发货
			orderService.dispatched(orderId, sellerId);
			int delayDay = new Random().nextInt(10);
			orderService.buyerDelayReceiveTime(orderId, sellerId, delayDay);
			Logger.comment("前置数据准备结束");
			// 验证推送消息
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.BuyerDelayReceive);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.BuyerDelayReceive);
			messageTask = scenarioMessageCall.getMessageTask();
			Logger.verifyIsNull(messageTask, "m2c买家延长收货,不发消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_MessageRoute_buyer_025() {
		Logger.start(true, "修改收货人地址");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额		
			// 支付订单
			orderService.orderPayRequest(userId, totalPrice, tradingId);
			// 接单
			orderService.acceptOrder(orderId, sellerId);
			// 接单后修改收货人地址
			ChangeOrderAddressBean coabean = new ChangeOrderAddressBean();
			ChangeOrderAddressCall coacall = new ChangeOrderAddressCall();
			String address = "南京路步行街";
			coabean.setAddress(address);
			String changeTime = YMTDateUtil.getSysTime();
			coabean.setChangeTime(changeTime);
			String email = TestDataManager.getEmail();
			coabean.setEmail(email);
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);
			coabean.setQQ("100001");
			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);
			String telephone = "021-51025140";
			coabean.setTelephone(telephone);
			coabean.setUserId(userId);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(false, coabean);
			Logger.comment("修改收货人地址");
			coacall.callService();
			Logger.comment("前置数据准备结束");
			// 验证推送消息
			String productName = "autotestxxxx";
			// 产品名称超过20位，截取20位
			if (productName.length() > 20) {
				productName = productName.substring(0, 20);
			}
			String messageDueTime = YMTDateUtil.getSysTime("yyyy-MM-dd");
			// call message service
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.ChangeOrderAddress);
			scenarioMessageCall = getScenarioMessageCallService(orderId,
					userId, messageDueTime, BusinessTypeEnum.ChangeOrderAddress);
			messageTask = scenarioMessageCall.getMessageTask();
			messageToolTaskList = messageTask.getMessageToolTasks();
			imMessage = scenarioMessageCall.getImMessage();
			Collections.reverse(messageToolTaskList);
			// 验证发送2个渠道
			Logger.verifyEquals(2, messageToolTaskList.size(),
					"验证c2c用户支付尾款后(卖家取消)，发送2个渠道");
			// 短信
			Logger.verifyEquals(2, messageToolTaskList.get(1).getMessageTool(),
					"验证c2c订单已取消，发短信");
			Logger.verifyIsContains(ECode.Message_Route_08, messageToolTaskList
					.get(1).getMessageBody(), "验证买手修改收货地址,短信内容包含"
					+ ECode.Message_Route_08);
			Logger.verifyEquals(true, messageToolTaskList.get(1).getSuccess(),
					"验证短信发送成功");
			// app 消息
			Logger.verifyEquals(1, messageToolTaskList.get(0).getMessageTool(),
					"验证c2c订单已取消，发app消息");
			Logger.verifyIsContains(ECode.Message_Route_08,
					imMessage.getTitle(), "验证买手修改收货地址,短信内容包含"
							+ ECode.Message_Route_08);
			Logger.verifyEquals(true, messageToolTaskList.get(0).getSuccess(),
					"验证消息发送成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}